package gw.com.android.utils;

import android.text.TextUtils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import www.com.library.app.Logger;

/**
 * @author jett
 * @since 2017-12-19.
 */
public class IOUtils {

    public static void closeSilent(Closeable c) {
        if (c != null) {
            try {
                c.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static boolean copy(File from, File to) {
        if (from == null || to == null) {
            return false;
        }
        InputStream is = null;
        OutputStream out = null;
        try {
            File parentFile = to.getParentFile();
            if (!parentFile.exists()) {
                boolean mkdirs = parentFile.mkdirs();
                if (!mkdirs) {
                    return false;
                }
            }
            if (!to.exists()) {
                boolean newFile = to.createNewFile();
                if (!newFile) {
                    return false;
                }
            }
            is = new FileInputStream(from);
            out = new FileOutputStream(to);
            byte[] buf = new byte[4096];
            int len;
            while ((len = is.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            out.flush();
            return true;
        } catch (IOException e) {
            return false;
        } finally {
            closeSilent(is);
            closeSilent(out);
        }
    }

    public static String readFile(File file) {
        if (file == null || !file.exists()) {
            return null;
        }
        BufferedReader reader = null;
        try {
            StringBuilder sb = new StringBuilder();
            reader = new BufferedReader(new FileReader(file));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (IOException ignore) {
        } finally {
            closeSilent(reader);
        }
        return null;
    }

    @SuppressWarnings("ResultOfMethodCallIgnored")
    public static void writeFile(File file, String content, boolean append) {
        if (TextUtils.isEmpty(content) || file == null){
            return;
        }
        File parentFile = file.getParentFile();
        if (!parentFile.exists()){
            parentFile.mkdirs();
        }
        BufferedWriter writer = null;
        try {
            if (!file.exists()){
                file.createNewFile();
            }
            writer = new BufferedWriter(new FileWriter(file, append));
            writer.write(content);
            writer.flush();
        } catch (IOException e) {
            Logger.e(e);
        } finally {
            closeSilent(writer);
        }
    }

}
